using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using UnityEngine;

public class StocksDataController
{
    public static string allStocksFilePath = Application.streamingAssetsPath + "/all_stocks.json";

    public static string stockTransitionsDir = Application.streamingAssetsPath + "/Transitions/";

    public static string stockZhuLiZiJinDir = Application.streamingAssetsPath + "/ZhuLiZiJin/";

    public static string stockZiJinLiuRuDir = Application.streamingAssetsPath + "/ZiJinLiuRu/";

    //public static string stock10DayZiJinLiuRuDir = Application.streamingAssetsPath + "/10DayZiJinLiuRu/";
    public static string stockJieDuanZhuLiDir = Application.streamingAssetsPath + "/JieDuanZhuLi/";

    //public static string stock10DayJieDuanZhuLiDir = Application.streamingAssetsPath + "/10DayJieDuanZhuLi/";
    public static string stockChengJiaoFenBuDir = Application.streamingAssetsPath + "/ChengJiaoFenBu/";

    //public static string stock10DayChengJiaoFenBuDir = Application.streamingAssetsPath + "/10DayChengJiaoFenBu/";
    public static string stockTransitionsFilePath(string stockCode) => stockTransitionsDir + $"{stockCode}.json";
    public static string stockZhuLiZiJinFilePath(string stockCode) => stockZhuLiZiJinDir + $"{stockCode}.json";
    public static string stockZiJinLiuRuFilePath(string stockCode) => stockZiJinLiuRuDir + $"{stockCode}.json";
    //public static string stock10DayZiJinLiuRuFilePath(string stockCode) => stock10DayZiJinLiuRuDir + $"{stockCode}.json";
    public static string stockJieDuanZhuLiFilePath(string stockCode) => stockJieDuanZhuLiDir + $"{stockCode}.json";
    //public static string stock10DayJieDuanZhuLiFilePath(string stockCode) => stock10DayJieDuanZhuLiDir + $"{stockCode}.json";
    public static string stockChengJiaoFenBuFilePath(string stockCode) => stockChengJiaoFenBuDir + $"{stockCode}.json";
    //public static string stock10DayChengJiaoFenBuFilePath(string stockCode) => stock10DayChengJiaoFenBuDir + $"{stockCode}.json";
    public static string stocksStatisticDir(int fromYear) => Application.streamingAssetsPath + $"/Statistics/{fromYear}_{System.DateTime.Now.Year}/";
    public static string stocksStatisticPath(int fromYear, string stockCode) => stocksStatisticDir(fromYear) + $"{stockCode}.json";
    
    static StocksDataController _instance;
    public static StocksDataController instance
    {
        get
        {
            if (_instance == null)
                _instance = new StocksDataController();
            return _instance;
        }
    }
    private  List<StockData> m_allStocks;
    public  List<StockData> allStocks
    {
        get
        {
            if (m_allStocks == null)
            {
                if(File.Exists(allStocksFilePath))
                    m_allStocks =  Newtonsoft.Json.JsonConvert.DeserializeObject<List<StockData>>(File.ReadAllText(allStocksFilePath));
                //m_allStocks = allStocks.FindAll(e => e.dm != "603395").AsList();
            }
            return m_allStocks;
            
        }
    }

    public  StockTranstions GetStockTransitions(string stockCode, int fromYear = 0)
    {
        var st = GetStockDatas<StockTranstions>(StocksDataController.stockTransitionsFilePath(stockCode));
        if (st == null) return null;
        if(fromYear > 0)
            st.transitionDatas = st.transitionDatas.AllWhere(e => int.Parse(e.d.Substring(0,4)) >= fromYear).AsList();
        return st;
    }   
    // public  StockZhuLiZiJin GetStockZhuLiZiJin(string stockCode, int fromYear = 0)
    // {
    //     var sz = GetStockDatas<StockZhuLiZiJin>(StocksDataController.stockZhuLiZiJinFilePath(stockCode));
    //     if(fromYear > 0)
    //         sz.zhulizijinDatas = sz.zhulizijinDatas.AllWhere(e => int.Parse(e.t.Substring(0,4)) >= fromYear).AsList();
    //     return sz;
    // }
    public  StockZiJinLiuRu GetStockZiJinLiuRu(string stockCode, int fromYear = 0)
    {
        var szj = GetStockDatas<StockZiJinLiuRu>(StocksDataController.stockZiJinLiuRuFilePath(stockCode));
        if (szj == null) return null;
        if(fromYear > 0)
            szj.ziJinLiuRuDatas = szj.ziJinLiuRuDatas.AllWhere(e => int.Parse(e.t.Substring(0,4)) >= fromYear).AsList();
        return szj;
    }
    public  StockJieDuanZhuLi GetStockJieDuanZhuLi(string stockCode, int fromYear = 0)
    {
        var sj = GetStockDatas<StockJieDuanZhuLi>(StocksDataController.stockJieDuanZhuLiFilePath(stockCode));
        if (sj == null) return null;
        if(fromYear > 0)
            sj.jieDuanZhuLiDatas = sj.jieDuanZhuLiDatas.AllWhere(e => int.Parse(e.t.Substring(0,4)) >= fromYear).AsList();
        return sj;
    }
    public  StockChengJiaoFenBu GetStockChengJiaoFenBu(string stockCode, int fromYear = 0)
    {
        var sc = GetStockDatas<StockChengJiaoFenBu>(StocksDataController.stockChengJiaoFenBuFilePath(stockCode));
        if (sc == null) return null;
        if(fromYear > 0)
            sc.chengJiaoFenBuDatas = sc.chengJiaoFenBuDatas.AllWhere(e => int.Parse(e.t.Substring(0,4)) >= fromYear).AsList();
        return sc;
    }
    public T GetStockDatas<T>(string path)where T : StockSubData
    {
        if (File.Exists(path))
        {
            return JsonConvert.DeserializeObject<T>(File.ReadAllText(path));
        }
        else
        {
            Debug.LogError($"数据文件不存在:{path} ");
            return null;
        }
    }

    public  StockData GetStock(string stockCode)
    {
        if (allStocks == null) return null;
        return allStocks.Find(e => e.dm == stockCode);
    }

    public StockAllData GetStockAllData(string stockCode, int fromYear = 0)
    {
        StockData stockData = GetStock(stockCode);
        if (stockData == null) return null;
        
        StockAllData stockAllData = new StockAllData();

        StockTranstions stockTransitions = GetStockTransitions(stockCode,fromYear);
        //StockZhuLiZiJin stockZhuLiZiJin = GetStockZhuLiZiJin(stockCode,fromYear);
      //  StockZiJinLiuRu stockZiJinLiuRu = GetStockZiJinLiuRu(stockCode,fromYear);
        StockJieDuanZhuLi stockJieDuanZhuLi = GetStockJieDuanZhuLi(stockCode,fromYear);
     //   StockChengJiaoFenBu stockChengJiaoFenBu = GetStockChengJiaoFenBu(stockCode,fromYear);

        stockAllData.stockData = stockData;
        if(stockTransitions != null)
            stockAllData.stockTransitions = stockTransitions.transitionDatas;
        //stockAllData.stockZhuLiZiJin = stockZhuLiZiJin.zhulizijinDatas;
       // if(stockZiJinLiuRu != null)
           // stockAllData.stockZiJinLiuRu = stockZiJinLiuRu.ziJinLiuRuDatas;
        if(stockJieDuanZhuLi != null)
          stockAllData.stockJieDuanZhuLi = stockJieDuanZhuLi.jieDuanZhuLiDatas;
      //  if(stockChengJiaoFenBu != null)
         //   stockAllData.stockChengJiaoFenBu = stockChengJiaoFenBu.chengJiaoFenBuDatas;

        stockAllData.Init();
        return stockAllData;
    }
    
    
}